package com.yix.admin.modules.system.entity;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.yix.common.base.BaseUUIDEntity;
import com.yix.common.utils.enums.DataScopeEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import java.io.Serializable;
import java.util.Objects;
import java.util.Set;

/**
 * @author wdx
 * @version 1.0
 * @Description TODO
 * @date 2021/1/27 17:08
 */
@Entity
@Getter
@Setter
@Table(name = "sys_role")
@org.hibernate.annotations.Table(appliesTo = "sys_role",comment = "角色表")
public class SysRole extends BaseUUIDEntity implements Serializable {

    @JSONField(serialize = false)
    @ManyToMany(mappedBy = "roles")
    @ApiModelProperty(value = "用户", hidden = true)
    private Set<SysUser> users;

    @ManyToMany
    @JSONField(serialize = false)
    @JoinTable(name = "roles_menus",
            joinColumns = {@JoinColumn(name = "role_id",referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "menu_id",referencedColumnName = "id")})
    @ApiModelProperty(value = "菜单", hidden = true)
    private Set<SysMenu> menus;

    @ManyToMany
    @JoinTable(name = "roles_depts",
            joinColumns = {@JoinColumn(name = "role_id",referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "dept_id",referencedColumnName = "id")})
    @ApiModelProperty(value = "部门", hidden = true)
    private Set<SysDept> depts;

    @NotBlank
    @Column(nullable = false,columnDefinition = "varchar(255) COMMENT '名称'")
    @ApiModelProperty(value = "名称")
    private String name;

    @Column(columnDefinition = "varchar(255) COMMENT '数据权限类型'")
    @ApiModelProperty(value = "数据权限类型 全部 、 本级 、 自定义")
    private String dataScope = DataScopeEnum.THIS_LEVEL.getValue();

    @Column(columnDefinition = "int(2) COMMENT '数值越小 级别越大'")
    @ApiModelProperty(value = "数值越小，级别越大")
    private Integer level = 3;

    @Column(columnDefinition = "varchar(255) COMMENT '备注'")
    @ApiModelProperty(value = "备注")
    private String remark;

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        SysRole role = (SysRole) o;
        return Objects.equals(id, role.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}
